﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using kim.Common;

namespace kim.Web.Admin.Photo
{
    public partial class AddorEdit : UI.ManagePage
    {
        protected string type;
        protected int typeid,fid;
        private int Nid;
        protected void Page_Load(object sender, EventArgs e)
        {
            labtitle.Text = "相册修改";
            typeid = KIMRequest.GetInt("typeid", 0);
            type = KIMRequest.GetString("type");
            fid = KIMRequest.GetInt("fid", 1);
            Nid = KIMRequest.GetInt("id", 1);
            if (!IsPostBack)
                GetRequestValues();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }
        }

        private void GetRequestValues()
        {
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.Photo().Delete(Nid);
                if (del)
                    Response.Redirect("List.aspx?typeid=" + typeid + "&fid=" + fid);
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");

        }
        private bool DoAdd()
        {
            Model.Photo mo = new Model.Photo();
            mo.MinUrl = this.txtImgUrl_min.Value;
            mo.BigUrl = this.txtImgUrl_max.Value;
            Headpic.Src = this.txtImgUrl_min.Value;
            mo.TypeID = typeid;
            mo.Title = txtTitle.Text.Trim();
            mo.Fid = fid;
            BLL.Photo bll = new BLL.Photo();
            if (bll.Add(mo) > 0)
                return true;
            else
                return false;
        }
        private bool DoEdit(int id)
        {
            Model.Photo mo = new Model.Photo();
            mo.Title = txtTitle.Text.Trim();
            mo.Fid = fid;
            mo.ID = id;
            mo.TypeID = typeid;
            mo.MinUrl = this.txtImgUrl_min.Value;
            mo.BigUrl = this.txtImgUrl_max.Value;
            Headpic.Src = this.txtImgUrl_max.Value;
            return new BLL.Photo().Update(mo);
        }
        private void GetEditValue()
        {
            Model.Photo model = new BLL.Photo().GetModel(Nid);
            txtTitle.Text = model.Title;
            Headpic.Src = model.BigUrl;
          
            this.txtImgUrl_max.Value = model.BigUrl; 
        }

        
    }
}